//
//  PigMarketApi.m
//  PigMarket
//
//  Created by Felix on 2019/4/11.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "PigMarketApi.h"

#import "Request.h"
#import "RequestHandler.h"
#import "NetworkConfigration.h"
#import "SystemUtil.h"

@implementation PigMarketApi

+(instancetype)sharedInstance {
    
    static PigMarketApi *sharedInstance = nil;
    static dispatch_once_t token;
    
    dispatch_once(&token,^{
        if(sharedInstance == nil){
            sharedInstance = [[PigMarketApi alloc] init];
        }
    } );
    
    return sharedInstance;
}


#pragma mark - 登录
- (RACSignal<UserInfo *> *)userLoginWithAccount:(NSString *)account
                                        msgCode:(NSString *)msgCode
                                      loginType:(NSNumber *)loginType {
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLUserLogin];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeNone;
    request.param = @{
                      @"number": account,
                      @"type": loginType,
                      @"deviceNum": [SystemUtil getUniqueId],
                      @"pwCode": msgCode
                      };
    request.responseType = [UserInfo class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 获取验证码
- (RACSignal<GetMessageCode *> *)getMessageCodeWithParam:(GetMessageCodeParam *)param {
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLUserGetVarifyCode];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeNone;
    request.param = param;
    request.responseType = [GetMessageCode class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 注册
- (RACSignal<UserInfo *> *)userRegisterWithAccount:(NSString *)account verifyCode:(NSString *)verifyCode {
    NetworkConfigration *configration = [NetworkConfigration defaultNetworkConfiguration];
    Request *request = [[Request alloc] init];
    request.URLString = [NSString stringWithFormat:@"%@%@",configration.baseApiURL,URLUserRegister];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeNone;
    NSMutableDictionary *param = [[NSMutableDictionary alloc] init];
    [param setObject:account forKey:@"account"];
    [param setObject:verifyCode forKey:@"code"];
    request.param = param;
    request.responseType = [UserInfo class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 三方注册
- (RACSignal<UserInfo *> *)userThirdRegisterWithAccount:(NSString *)account verifyCode:(NSString *)verifyCode wechatId:(NSString *)wechatId qqId:(NSString *)qqId sinaId:(NSString *)sinaId {
    NetworkConfigration *configration = [NetworkConfigration defaultNetworkConfiguration];
    Request *request = [[Request alloc] init];
    request.URLString = [NSString stringWithFormat:@"%@%@",configration.baseApiURL,URLUserRegister];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeNone;
    request.param = @{@"account":account,
                      @"code":verifyCode,
                      @"wxOpenId":wechatId,
                      @"qqOpenId":qqId,
                      @"wbOpenId":sinaId};
    request.responseType = [UserInfo class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 邀请人列表
- (RACSignal<SelectInviterList *> *)getSelectInviterListWithLongitude:(NSString *)longitude latitude:(NSString *)latitude {
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLGetSelectInviterList];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeNone;
    request.param = @{@"longitude":longitude,
                      @"latitude":latitude};
    request.responseType = [SelectInviterList class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 选择邀请人邀请码
- (RACSignal<NSNull *> *)userSelectInviteCodeWithCode:(NSString *)code account:(NSString *)account{
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLSelectInviteCode];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeNone;
    request.param = @{@"account":account,
                      @"inviteCode":code};
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - 完善个人资料
- (RACSignal<NSNull *> *)userPersonalInformationWithParam:(PersonalInformationParam *)param {
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLPersonalInformation];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeNone;
    request.param = param;
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - 上传图片
- (RACSignal<PhotosList *> *)uploadPhotosWithPhotosArr:(NSMutableArray *)photoArr account:(NSString *)account type:(UpDateImageType) type{
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLUploadPhotos];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeNone;
    NSMutableArray *paramArr = [[NSMutableArray alloc] init];
    for (int i =0; i < photoArr.count; i++) {
        NSData *data = UIImageJPEGRepresentation(photoArr[i], 0.5);
        NSString *photoStr = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
        NSString *fileName = [NSString stringWithFormat:@"filename_%d",i];
        NSDictionary *dic = @{@"img":photoStr,
                              @"type":@"png",
                              @"fileName":fileName,
                              };
        [paramArr addObject:dic];
    }
    
    //
    NSString *memberId = [NSString stringWithFormat:@"%@",[UserInfoDataProvider sharedInstance].userInfo.memberId];
    
    //状态
    NSNumber *state = 0;
    switch (type) {
        case UpDateImageType_Header:
            state = @1;
            break;
            
        case UpDateImageType_Photo:
            state = @2;
            
        default:
            state = @0;
            break;
    }
    
    request.param = @{@"imgs":paramArr,
                      @"memberId": memberId,
                      @"account":account,
                      @"state":state
                      };
    request.responseType = [PhotosList class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 设置密码
- (RACSignal<NSNull *> *)userSetUpPasswordWithAccount:(NSString *)account password:(NSString *)password {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLSetUpPassword];
    request.oauthType = OAuthTypeNone;
    request.param = @{@"account":account,@"loginPwd":password};
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}
#pragma mark - 获取意见反馈标签列表
- (RACSignal<ComplaintFeedbackList *> *)getCompaintFeedbackList {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodGET;
    request.URLString = [self getFullURLString:URLGetComplaintFeedbookList];
    request.oauthType = OAuthTypeMember;
    request.responseType = [ComplaintFeedbackList class];
    return [self createSignalWithReuqest:request];
}
#pragma mark - 提交获取意见反馈
- (RACSignal<NSNull *> *)commitComplaintFeedbackWith:(ComplaintFeedbackParam *)param {
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLCommotComplaintFeedbook];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = param;
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}
#pragma mark - 注销
- (RACSignal <NSNull *> *)userAccountWriteOffWithMemberId:(NSNumber *)memberId {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLWriteOff];
    request.oauthType = OAuthTypeMember;
    request.param = @{@"memberId":memberId};
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}
#pragma mark - 修改密码
- (RACSignal<NSNull *> *)modifyPasswordWithMemberId:(NSNumber *)memberId oldPassword:(NSString *)oldPassword newPassword:(NSString *)newPassword {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLModifyPassword];
    request.oauthType = OAuthTypeMember;
    request.param = @{@"memberId":memberId,
                      @"oldPassword":oldPassword,
                      @"newPassword":newPassword};
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}
#pragma mark - 实名认证
- (RACSignal<RealName *> *)getRealNameMessageWithMemberId:(NSNumber *)memberId {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodGET;
    NSString *url = [NSString stringWithFormat:@"%@/%@", URLRealName, memberId];
    request.URLString = [self getFullURLString:url];
    request.oauthType = OAuthTypeMember;
    request.responseType = [RealName class];
    return [self createSignalWithReuqest:request];
}
#pragma mark - 邀请好友
- (RACSignal <InviteFriends *> *)inviteFriendsWithMemberId:(NSNumber *)memberId {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodGET;
    request.URLString = [self getFullURLString:[NSString stringWithFormat:@"%@/%@",URLInviteFriends,memberId]];
    request.oauthType = OAuthTypeMember;
    request.responseType = [InviteFriends class];
    return [self createSignalWithReuqest:request];
}
#pragma mark - 常见问题
- (RACSignal<CommonQuestionsList *> *)getCommonQuestionsWithQuestionType:(NSNumber *)questionType {
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:[NSString stringWithFormat:@"%@/%@",URLCommonQuestions,questionType]];
    request.method = kHTTPMethodGET;
    request.oauthType = OAuthTypeMember;
    request.responseType = [CommonQuestionsList class];
    return [self createSignalWithReuqest:request];
}
#pragma mark - 增加、编辑地址
- (RACSignal<AddressList *> *)getAddressListWithMemberId:(NSNumber *)memberId {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodGET;
    request.URLString = [self getFullURLString:[NSString stringWithFormat:@"%@/%@",URLAddressList,memberId]];
    request.oauthType = OAuthTypeMember;
    request.responseType = [AddressList class];
    return [self createSignalWithReuqest:request];
}
#pragma mark - 增加地址
- (RACSignal<NSNull *> *)addAddressWithParam:(AddressParam *)param {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLAddAddress];
    request.oauthType = OAuthTypeMember;
    request.param = param;
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}
#pragma mark - 编辑地址
- (RACSignal<NSNull *> *)editAddressWithParam:(AddressParam *)param {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLEditAddress];
    request.oauthType = OAuthTypeMember;
    request.param = param;
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}
#pragma mark - 删除地址
- (RACSignal <NSNull *> *)deleteAddressWithReceiveAddressId:(NSString *)receiveAddressId {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLDeleteAddress];
    request.oauthType = OAuthTypeMember;
    request.param = @{@"receiveAddressId":receiveAddressId};
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}
#pragma mark - 我的个人信息
- (RACSignal <UserDetailInfo *> *)getUserDetailInfoWithMemberId:(NSNumber *)memberId {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodGET;
    request.URLString = [self getFullURLString:[NSString stringWithFormat:@"%@/%@",URLUserDetailInfo,memberId]];
    
    request.oauthType = OAuthTypeMember;
    request.responseType = [UserDetailInfo class];
    return [self createSignalWithReuqest:request];
}
#pragma mark - 修改个人资料
- (RACSignal <NSNull *> *)modifyUserInfoWithParam:(PersonalInformationParam *)param {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLModifyUserInfo];
    request.oauthType = OAuthTypeMember;
    request.param = param;
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - 获取照片
- (RACSignal<PhotosList *> *)getPhotosWithMemberId:(NSNumber *)memberId {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodGET;
    request.URLString = [self getFullURLString:[NSString stringWithFormat:@"%@/%@",URLGetPhotos,memberId]];
    request.oauthType = OAuthTypeMember;
    request.responseType = [PhotosList class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 删除照片
- (RACSignal<NSNull *> *)deletephotoWithMemberId:(NSNumber *)memberId {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLDeletePhoto];
    request.oauthType = OAuthTypeMember;
    request.param = @{@"meimgId":memberId};
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - 更新图片
- (RACSignal<NSNull *> *)updatePhotosWithMemberId:(NSNumber *)memberId imgNameArr:(NSMutableArray *)imgNameArr {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLUpdatePhotos];
    request.oauthType = OAuthTypeMember;
    request.param = @{@"memberId":memberId,
                      @"imgs":imgNameArr};
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - 获取首页轮播
- (RACSignal<MessageList *> *)getAdoptBanner {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodGET;
    request.URLString = [self getFullURLString:URLGetAdoptBanner];
    request.oauthType = OAuthTypeNone;
    request.responseType = [MessageList class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 首页豚券消息
- (RACSignal <MessageList *> *)adoptMessageWithMemberId:(NSNumber *)memberId {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLAdoptMessage];
    request.oauthType = OAuthTypeMember;
    request.param = @{@"memberId":memberId};
    request.responseType = [MessageList class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 首页未领养猪的品种
- (RACSignal <PigVarietyList *> *)getPigVariety {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodGET;
    request.URLString = [self getFullURLString:URLPigVariety];
    request.oauthType = OAuthTypeNone;
    request.responseType = [PigVarietyList class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 获取城市区域列表
- (RACSignal <SelectTribesList *> *)areasListWithParentId:(NSNumber *)parentId {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLAreasList];
    request.oauthType = OAuthTypeMember;
    request.param = @{@"parentId":parentId};
    request.responseType = [SelectTribesList class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 获取部落列表
- (RACSignal <SelectTribesList *> *)tribeListWithParentId:(NSNumber *)parentId {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLTribeList];
    request.oauthType = OAuthTypeMember;
    request.param = @{@"regionId":parentId};
    request.responseType = [SelectTribesList class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 猪的列表
- (RACSignal<AdoptList *> *)AdoptPigListWithParam:(AdoptParam *)param {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLAdoptPigList];
    request.oauthType = OAuthTypeMember;
    request.param = param;
    request.responseType = [AdoptList class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 绑定和解绑
- (RACSignal <NSNull *> *)accountBindOrUnbindWithParam:(AccountBindParam *)param {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLBindOrUnbind];
    request.oauthType = OAuthTypeMember;
    request.param = param;
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - 小猪领养详情
- (RACSignal<AdoptDetailList *> *)adoptDetailWithPigId:(NSNumber *)pigId tribeId:(NSNumber *)tribeId {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodGET;
    request.URLString = [self getFullURLString:[NSString stringWithFormat:@"%@/%@/%@",URLAdoptDetail,pigId,tribeId]];
    request.oauthType = OAuthTypeMember;
    request.param = @{@"pigId":pigId,
                      @"tribeId":tribeId};
    request.responseType = [AdoptDetailList class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 余额查询
- (RACSignal <Pay *> *)getBalanceWithMemberId:(NSNumber *)memberId {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodGET;
    request.URLString = [self getFullURLString:[NSString stringWithFormat:@"%@/%@",URLGetBalance,memberId]];
    request.oauthType = OAuthTypeMember;
    request.responseType = [Pay class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 支付
- (RACSignal<Pay *> *)payWithParam:(PayParam *)param {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLPay];
    request.oauthType = OAuthTypeMember;
    request.param = param;
    request.responseType = [Pay class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 充值
- (RACSignal <Pay *> *)topUpRequestWithParam:(TopUpMoneyParam *)param {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLTopUpMoney];
    request.oauthType = OAuthTypeMember;
    request.param = param;
    request.responseType = [Pay class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 充值结果校验
- (RACSignal <Pay *> *)topUpResultWithMemberId:(NSNumber *)memberId money:(NSString *)money{
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLCheckTopUpMoneyResult];
    request.oauthType = OAuthTypeMember;
    request.param = @{@"memberId":memberId,
                      @"money":money
                      };
    request.responseType = [Pay class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 支付宝 微信支付结果校验

- (RACSignal<Pay *> *)thirdPayWithMemberId:(NSNumber *)memberId pigId:(NSNumber *)pigId {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodGET;
    request.URLString = [self getFullURLString:[NSString stringWithFormat:@"%@%@/%@",URLThirdPay,memberId,pigId]];//@"http://39.98.179.212:8686/pig-joggle/pay/test";
    request.oauthType = OAuthTypeMember;
    request.responseType = [Pay class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 获取用户豚券
- (RACSignal<DiscountCouponsList *> *)userDiscountCouponsWithParam:(DiscountCouponsParam *)param {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLDiscountCoupons];
    request.oauthType = OAuthTypeMember;
    request.param = param;
    request.responseType = [DiscountCouponsList class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 修改支付密码
- (RACSignal<NSNull *> *)modifyPayPasswordWithMemberId:(NSNumber *)memberId payPwd:(NSString *)payPwd {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLModifyPayPassword];
    request.oauthType = OAuthTypeMember;
    request.param = @{@"memberId":memberId,
                      @"payPwd":payPwd
                      };
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - 已领养列表
- (RACSignal <AdoptedList *> *)AdoptedListWithParam:(AdoptedParam *)param {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLAdopted];
    request.oauthType = OAuthTypeMember;
    request.param = param;
    request.responseType = [AdoptedList class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 转让猪的信息
- (RACSignal <TransferInfo *> *)transferPigInfoWithAdoptPlanId:(NSNumber *)adoptPlanId {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLTransferPigInfo];
    request.oauthType = OAuthTypeMember;
    request.param = @{@"adoptPlanId":adoptPlanId};
    request.responseType = [TransferInfo class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 确认转让
- (RACSignal <NSNull *> *)confirmTransferWithMemberId:(NSNumber *)memberId receiverPhone:(NSString *)receiverPhone transferMoney:(NSString *)transferMoney pigId:(NSNumber *)pigId {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLConfirmTransfer];
    request.oauthType = OAuthTypeMember;
    request.param = @{@"memberId":memberId,
                      @"phone":receiverPhone,
                      @"money":transferMoney,
                      @"pigId":pigId};
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - 转让管理
- (RACSignal <TransferManagerList *> *)transferManagerWithParam:(TransferManagerParam *)param {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLTransferManager];
    request.oauthType = OAuthTypeMember;
    request.param = param;
    request.responseType = [TransferManagerList class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 撤销转让
- (RACSignal <NSNull *> *)cancelTransferWithAssignmentId:(NSNumber *)assignmentId {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLCancelTransfer];
    request.oauthType = OAuthTypeMember;
    request.param = @{@"assignmentId":assignmentId};
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - 接收
- (RACSignal <ReceivePigList *> *)receivePigWithMemberId:(NSNumber *)memberId {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLReceivePig];
    request.oauthType = OAuthTypeMember;
    request.param = @{@"memberId":memberId};
    request.responseType = [ReceivePigList class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 拒绝 同意接收
- (RACSignal <NSNull *> *)receiveStatusWithAssignmentId:(NSNumber *)assignmentId type:(NSNumber *)type payType:(NSNumber *)payType{
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLReceiveStatus];
    request.oauthType = OAuthTypeMember;
    request.param = @{@"assignmentId":assignmentId,
                      @"type":type,
                      @"payType":payType
                      };
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - 同意接收 支付前获取参数
- (RACSignal <Pay *> *)receivePigToGetThirdPayParamWithParam:(GetThirdPayParam *)param {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLReceivePigToGetThirdPayParam];
    request.oauthType = OAuthTypeMember;
    request.param = param;
    request.responseType = [Pay class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 已领养套餐页面
- (RACSignal <AdoptedTrusteeList *> *)adoptedTrusteeWithAdoptPlanId:(NSNumber *)adoptPlanId {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLAdoptedTrustee];
    request.oauthType = OAuthTypeMember;
    request.param = @{@"adoptPlanId":adoptPlanId};
    request.responseType = [AdoptedTrusteeList class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 已领养支付页面，获取头部数据、三方支付参数
- (RACSignal <Pay *> *)adoptedPayWithParam:(AdoptedPayParam *)param {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLAdoptedPayInfo];
    request.oauthType = OAuthTypeMember;
    request.param = param;
    request.responseType = [Pay class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 已领养 支付结果校验接口
- (RACSignal <Pay *> *)checkAdoptedPayRequestWithAdoptPlanId:(NSNumber *)adoptPlanId orderNum:(NSString *)orderNum {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLCheckAdoptedPayRequest];
    request.oauthType = OAuthTypeMember;
    request.param = @{@"adoptPlanId":adoptPlanId,
                      @"ordernum":orderNum};
    request.responseType = [Pay class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 获取部落范围的经纬度
- (RACSignal <TribeRangeList *> *)getTribeRange {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLTribeRange];
    request.oauthType = OAuthTypeMember;
    request.responseType = [TribeRangeList class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 我的资产
- (RACSignal <MyAsset *> *)getMyAssetWithMemberId:(NSNumber *)memberId {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLMyAsset];
    request.oauthType = OAuthTypeMember;
    request.param = @{@"memberId":memberId};
    request.responseType = [MyAsset class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 资产交易明细
- (RACSignal <TransicationDetailList *> *)tracnsicationDetailWithParam:(TransicationDetailParam *)param {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLTransicationDetail];
    request.oauthType = OAuthTypeMember;
    request.param = param;
    request.responseType = [TransicationDetailList class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 提现
- (RACSignal <NSNull *> *)withdrawWithMemberId:(NSNumber *)memberId money:(NSString *)money payPwd:(NSString *)payPwd{
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLWithdraw];
    request.oauthType = OAuthTypeMember;
    request.param = @{@"memberId":memberId,
                      @"money":money,
                      @"payPwd":payPwd
                      };
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - 提现  输入支付宝账号
- (RACSignal <NSNull *> *)alipayAccountWithMemberId:(NSNumber *)memberId account:(NSString *)account code:(NSString *)code money:(NSString *)money{
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLAlipayAccount];
    request.oauthType = OAuthTypeMember;
    request.param = @{@"memberId":memberId,
                      @"openId":account,
                      @"code":code,
                      @"money":money
                      };
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - 支付密码校验
- (RACSignal <NSNull *> *)checkPayPasswordWithMemberId:(NSNumber *)memberId payPwd:(NSString *)payPwd {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLCheckPayPassword];
    request.oauthType = OAuthTypeMember;
    request.param = @{@"memberId":memberId,
                      @"payPwd":payPwd};
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - 支付宝历史提现账号记录
- (RACSignal <WithdrawAlipayAccountHistoryRecord *> *)getWithdrawAlipayAccoutnHistoryRecordWithMemberId:(NSNumber *)memberId {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodGET;
    request.URLString = [self getFullURLString:[NSString stringWithFormat:@"%@/%@",URLWithdrawAlipayHistoryRecord,memberId]];
    request.oauthType = OAuthTypeMember;
    request.responseType = [WithdrawAlipayAccountHistoryRecord class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 获取用户标签
- (RACSignal <UserTagsList *> *)getUserTags {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodGET;
    request.URLString = [self getFullURLString:URLGetUserTags];
    request.oauthType = OAuthTypeMember;
    request.responseType = [UserTagsList class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 我的好友列表
- (RACSignal <FriendsList *> *)myFriendsListWithMemberId:(NSNumber *)memberId nickName:(NSString *)nickName currentPage:(int)page {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLMyFriendList];
    request.oauthType = OAuthTypeMember;
    request.param = @{@"page":@(page),
                      @"limit":@10,
                      @"memberId":memberId,
                      @"nickName":nickName
                      };
    request.responseType = [FriendsList class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 附近豚友
- (RACSignal <NearbyFriendsList *> *)nearbyFriendsWithParam:(NearbyFriendsParam *)param {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLNearbyFriends];
    request.oauthType = OAuthTypeMember;
    request.param = param;
    request.responseType = [NearbyFriendsList class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 添加好友
- (RACSignal <NSNull *> *)agreeAddFriendWithFromMemberNum:(NSString *)fromMemberNum toMemberNum:(NSString *)toMemberNum {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLAgreeAddFriend];
    request.oauthType = OAuthTypeMember;
    request.param = @{@"fromMemberNum":fromMemberNum,
                      @"toMemberNum":toMemberNum};
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - 搜索好友添加
- (RACSignal <SearchAddFriendList *> *)searchAddFriendWithSearchText:(NSString *)searchText {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLSearchAddFriend];
    request.oauthType = OAuthTypeMember;
    request.param = @{@"textstr":searchText};
    request.responseType = [SearchAddFriendList class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 聊天获取个人资料
- (RACSignal <ChatUserInfo *> *)getChatUserInfoWithMemberNum:(NSString *)memberNum myMemberNum:(NSString *)myMemberNum {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodGET;
    request.URLString = [self getFullURLString:[NSString stringWithFormat:@"%@/%@/%@",URLChatUserInfo,memberNum,myMemberNum]];
    request.oauthType = OAuthTypeMember;
    request.responseType = [ChatUserInfo class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 删除好友
- (RACSignal <NSNull *> *)deleteChatFriendWithMemberId:(NSNumber *)memberId friendId:(NSNumber *)friendId {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLDeleteFriend];
    request.oauthType = OAuthTypeMember;
    request.param = @{@"memberId":memberId,
                      @"friendId":friendId};
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - 添加黑名单
- (RACSignal <NSNull *> *)addBlackListWithMyMemberNum:(NSString *)myMemberNum memberNum:(NSString *)memberNum {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLAddBlackList];
    request.oauthType = OAuthTypeMember;
    request.param = @{@"myMemberNum":myMemberNum,
                      @"memberNum":memberNum};
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - 删除黑名单
- (RACSignal <NSNull *> *)deleteBlackListWithMyMemberNum:(NSString *)myMemberNum memberNum:(NSString *)memberNum {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLDeleteBlackList];
    request.oauthType = OAuthTypeMember;
    request.param = @{@"myMemberNum":myMemberNum,
                      @"memberNum":memberNum};
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - 修改好友备注
- (RACSignal <NSNull *> *)modifyFriendNameWithMemberId:(NSNumber *)memberId memberNum:(NSString *)memberNum noteName:(NSString *)noteName {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLModifyFriendName];
    request.oauthType = OAuthTypeMember;
    request.param = @{@"memberId":memberId,
                      @"memberNum":memberNum,
                      @"noteName":noteName};
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - 删除群成员
- (RACSignal <NSNull *> *)deleteGroupMemberWithGroupId:(NSString *)groupId memberList:(NSMutableArray *)memberList {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLDeleteGroupMember];
    request.oauthType = OAuthTypeMember;
    request.param = @{@"groupId":groupId,
                      @"memberList":memberList};
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - 添加群成员
- (RACSignal <NSNull *> *)addGroupMemberWithGroupId:(NSString *)groupId memberList:(NSMutableArray *)memberList {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLAddGroupMember];
    request.oauthType = OAuthTypeMember;
    request.param = @{@"groupId":groupId,
                      @"memberList":memberList};
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - 聊天投诉类型列表
- (RACSignal <ChatComplaintList *> *)getChatComplaint {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodGET;
    request.URLString = [self getFullURLString:URLChatComplaintTypeList];
    request.oauthType = OAuthTypeMember;
    request.responseType = [ChatComplaintList class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 投诉
- (RACSignal <NSNull *> *)chatComplaintWithParam:(ChatComplaintParam *)param {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLChatComplaint];
    request.oauthType = OAuthTypeMember;
    request.param = param;
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - 上传手机通讯录
- (RACSignal <NSNull *> *)uploadAddressBookWithFileName:(NSString *)fileName file:(id)file {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLUploadAddressBook];
    request.oauthType = OAuthTypeMember;
    request.param = @{@"fileName":fileName,
                      @"file":file};
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - 看看他的详细信息
- (RACSignal <LookList *> *)getLookDetailInfoWithPigId:(NSNumber *)pigId {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodGET;
    request.URLString = [self getFullURLString:[NSString stringWithFormat:@"%@/%@",URLLookDetailInfo,pigId]];
    request.oauthType = OAuthTypeMember;
    request.responseType = [LookList class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 发现页的广告
- (RACSignal <MessageList *> *)getDiscoveryAdvertising {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodGET;
    request.URLString = [self getFullURLString:[NSString stringWithFormat:@"%@/%@",URLDiscoveryAdvertising,@(4)]];
    request.oauthType = OAuthTypeMember;
    request.responseType = [MessageList class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 小猪档案
- (RACSignal <PigRecord *> *)pigRecordWithPigId:(NSNumber *)pigId memberId:(NSNumber *)memberId {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLPigRecord];
    request.oauthType = OAuthTypeMember;
    request.param = @{@"pigId":pigId,
                      @"memberId":memberId};
    request.responseType = [PigRecord class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 生长报告
- (RACSignal <GrowRecordAllList *> *)growRecordWithPigId:(NSNumber *)pigId {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLGrowRecord];
    request.oauthType = OAuthTypeMember;
    request.param = @{@"pigId":pigId};
    request.responseType = [GrowRecordAllList class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 生长报告详情
- (RACSignal <GrowRecordDetail *> *)growRecordDetailWithReportId:(NSNumber *)reportId {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLGrowRecordDetail];
    request.oauthType = OAuthTypeMember;
    request.param = @{@"pigGrowReportId":reportId};
    request.responseType = [GrowRecordDetail class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 同栏豚友
- (RACSignal <SamePenFriendList *> *)samePenFriendWithPigId:(NSNumber *)pigId memberId:(NSNumber *)memberId page:(int)page {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodPOST;
    request.URLString = [self getFullURLString:URLSamePenFriend];
    request.oauthType = OAuthTypeMember;
    request.param = @{@"pigId":pigId,
                      @"memberId":memberId,
                      @"page":@(page),
                      @"limit":@10};
    request.responseType = [SamePenFriendList class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 切换账号list接口
- (RACSignal <LoginAccountModel *> *)getLoginAccountList {
    Request *request = [[Request alloc] init];
    request.method = kHTTPMethodGET;
    request.URLString = [self getFullURLString:[NSString stringWithFormat:@"%@/%@",URLLoginSwitchAccountList,[SystemUtil getUniqueId]]];
    request.oauthType = OAuthTypeMember;
    request.responseType = [LoginAccountModel class];
    return [self createSignalWithReuqest:request];
}



- (NSString *)getFullURLString:(NSString *)URLString {
    NetworkConfigration *configration = [NetworkConfigration defaultNetworkConfiguration];
    return [NSString stringWithFormat:@"%@%@", configration.baseApiURL, URLString];
}


- (RACSignal *)createSignalWithReuqest:(Request *) request {
    
    
    return [[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        
        RequestHandler *handler = [[RequestHandler alloc] initWithRequest:request responseCallback:^(Response *response) {
            

#pragma mark - 回参数
            if (response.code == ResultCodeSuccess) {
                
                if(request.responseType) {
                    [subscriber sendNext:response.data];
                } else {
                    [subscriber sendNext:[NSNull null]];
                }
                [subscriber sendCompleted];
                
            } else {
                if (response.code == ResultCodeDeviceChanged) {//更换设备
                    UserInfo *userInfo = [UserInfoDataProvider sharedInstance].userInfo;
                    userInfo.isLogin = NO;
                    [UserInfoDataProvider sharedInstance].userInfo = userInfo;
                    
                }
                NSError *error = [NSError errorWithDomain:@"com.naturalgrowth.pigmarket.ErrorDomain" code:response.code userInfo: @{ NSLocalizedDescriptionKey : response.msg }];
                [subscriber sendError:error];
            }
            
        }];

        [handler sendRequest];
        
        return [RACDisposable disposableWithBlock:^{
            
            [handler cancelRequest];
        }];
    }] subscribeOn:[RACScheduler scheduler]] ;
}

@end
